package com.algorithm.data_structure;

import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

@Slf4j
public class PrintLinkSulotionST02 {
    public int[] reversePrint(ListNode head) {
        int size = 0;
        ListNode tmp = head;
        while (tmp != null) {
            size++;
            tmp = tmp.next;
        }

        int[] result = new int[size];

        for (int i = size - 1; i >= 0; i--) {
            result[i] = head.val;
            head = head.next;
        }
        return result;
    }

    @Test
    @DisplayName("输入一个链表的头节点，从尾到头反过来返回每个节点的值（用数组返回）。")
    public void test() {
        ListNode l0 = new ListNode(1);
        ListNode l1 = new ListNode(2);
        ListNode l2 = new ListNode(3);
        l0.next = l1;
        l1.next = l2;

        int[] ints = reversePrint(l0);
        log.info("result is ");
    }
}

class ListNode {
    int val;
    ListNode next;

    ListNode(int x) {
        val = x;
    }
}
